//
//  XGVideoNewsCell.swift
//  TodayNews
//
//  Created by monkey on 2020/3/27.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import Kingfisher

class XGVideoNewsCell: UITableViewCell, XGRegisterReusableViewProtocol, XGNewsProtocol
{
    var newsViewModel: XGNewsViewModel? {
        didSet {
            userNameLabel.text = newsViewModel?.userInfo?.name
            titleLabel.text = newsViewModel?.title
            watchCountLabel.text = newsViewModel?.videoWatchCount
            durationLabel.text = newsViewModel?.videoDuration
            if newsViewModel?.middleImage != nil {
                coverImageView.kf.setImage(with: URL(string: newsViewModel!.middleImage!))
            }
            if newsViewModel?.userInfo?.avatar_url != nil {
                userIconImageView.kf.setImage(with: URL(string: newsViewModel!.userInfo!.avatar_url!), options: [.processor(RoundCornerImageProcessor(cornerRadius:18, targetSize: CGSize(width: 36, height: 36)))])
            }
        }
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 私有属性
    
    /// 用户头像
    private lazy var userIconImageView = UIImageView()
    /// 用户昵称
    private lazy var userNameLabel = UILabel(text: "测试", textColor: UIColor.darkGray, fontSize: 15, textAlignment: .left)
    /// 背景图片
    private lazy var coverImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        imageView.backgroundColor = UIColor.black
        return imageView
    }()
    /// 标题
    private lazy var titleLabel = UILabel(text: "测试", textColor: UIColor.white, fontSize: 20, textAlignment: .left)
    /// 描述信息
    private lazy var watchCountLabel = UILabel(text: "测试", textColor: UIColor.white, fontSize: 15, textAlignment: .center)
    /// 播放时长
    private lazy var durationLabel: UILabel = {
        let label = UILabel(text: "测试", textColor: UIColor.white, fontSize: 15, textAlignment: .center)
        label.backgroundColor = UIColor(white: 0, alpha: 0.4)
        return label
    }()
    /// 播放按钮
    private lazy var playVideoButton = UIButton(backgroundImageName: "video_play_icon_44x44_", target: nil, action: nil)
}

// MARK: - 私有扩展

private extension XGVideoNewsCell
{
    func setUpUI() -> Void
    {
        backgroundColor = UIColor.white
        contentView.addSubview(userIconImageView)
        contentView.addSubview(userNameLabel)
        contentView.addSubview(coverImageView)
        contentView.addSubview(titleLabel)
        contentView.addSubview(watchCountLabel)
        contentView.addSubview(durationLabel)
        contentView.addSubview(playVideoButton)
        
        userIconImageView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(5)
            make.left.equalToSuperview().offset(10)
            make.size.equalTo(CGSize(width: 36, height: 36))
        }
        userNameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(userIconImageView.snp.right).offset(10)
            make.centerY.equalTo(userIconImageView)
            make.right.lessThanOrEqualToSuperview().offset(-10)
        }
        coverImageView.snp.makeConstraints { (make) in
            make.top.equalTo(userIconImageView.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(ceil(TodayNews_Screen_Width / 16.0 * 9.0))
        }
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(coverImageView).offset(5)
            make.left.equalTo(coverImageView).offset(10)
            make.right.equalTo(coverImageView).offset(-10)
            make.height.lessThanOrEqualTo(50)
        }
        watchCountLabel.snp.makeConstraints { (make) in
            make.left.equalTo(coverImageView).offset(10)
            make.bottom.equalTo(coverImageView).offset(-5)
        }
        durationLabel.snp.makeConstraints { (make) in
            make.right.equalTo(coverImageView).offset(-10)
            make.bottom.equalTo(coverImageView).offset(-5)
            make.size.equalTo(CGSize(width: 60, height: 20))
        }
        playVideoButton.snp.makeConstraints { (make) in
            make.center.equalTo(coverImageView)
            make.size.equalTo(CGSize(width: 44, height: 44))
        }
    }
}
